﻿///////////////////////////////////////////////////////////////
//                                                           //
// Code from Arcanea Project.                                //
//                                                           //
// Please include this license part to every reproduction    //
//   of code.                                                //
//                                                           //
// This code is under Ms-RL license                          //
//                                                           //
// Please read futher information about the license,         //
//   the project (news and updates) and the                  //
//   contributors on the address :                           //
//     http://www.codeplex.com/Arcanea/                      //
//                                                           //
///////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using Arcanea.NetworkEngine.Abstract.Engines;
using System.Net.Sockets;
using Arcanea.Mediator.Exceptions;

namespace Arcanea.NetworkEngine.Concrete.Engines.Protocole
{
    /// <summary>
    /// Protocole engine de connection TCP client
    /// </summary>
    public class TCPClientProtocoleEngine : IProtocoleEngine
    {

        public TCPClientProtocoleEngine(string serveur, int port)
        {
            netInformations.Server = serveur;
            netInformations.PortPrincipal = port;
        }

        public TCPClientProtocoleEngine(NetInformationContainer container)
        {
            netInformations = container;
        }

        #region Functions non utilisées

        public override bool Bind()
        {
            throw new BadFunctionUseException();
        }

        public override NetInformationContainer AcceptedConnection()
        {
            throw new BadFunctionUseException();
        }

        #endregion

        #region Functions utilisées

        public override bool Connect()
        {
            netInformations.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            netInformations.Socket.Connect(netInformations.Server, netInformations.PortPrincipal);
            return true;
        }

        public override bool Send(byte[] array)
        {
            if (netInformations.Socket == null || !netInformations.Socket.Connected) return false;
            try
            {
                netInformations.Socket.Send(array);
            }
            catch (Exception e)
            {
                logger.Error("Unable to send message : " + e.Message);
                return false;
            }
            return true;
        }

        public override byte[] Receive(int taille)
        {
            if (netInformations.Socket == null || !netInformations.Socket.Connected) return null;
            byte[] a = new byte[taille];
            int readen = 0;

            try
            {
                readen = netInformations.Socket.Receive(a, 0, taille, SocketFlags.None);
            }
            catch (Exception e)
            {
                logger.Error("Unable to receive message : " + e.Message);
                return null;
            }

            if (readen == taille)
            {
                return a;
            }
            else
            {
                return null;
            }
        }

        #endregion

        #region Private functions

        #endregion

    }
}
